const exe_chrome="/usr/bin/chromium"
const puppeteer = require("puppeteer");
const fs = require("fs");
//const cookie_path = "file:///E:/AGI/AutoUP/.confidential/bilibili_cookies_std.json";
const cookie_path = "file:///root/AGI/AutoUP/.confidential/bilibili_cookies_std.json";
const mainURL = "https://message.bilibili.com/#/whisper/";
const prefix = "https://message.bilibili.com/#/whisper/";
const midlist = [];
// read it from json.

function getCenter(str){var obj = JSON.parse(str);var x0 = obj["x"]+(obj["width"]/2);var y0 = obj["y"]+(obj["height"]/2);return {"x0":Math.round(x0),"y0":Math.round(y0)};}

(async () => {
const browser =  await puppeteer.launch({executablePath: exe_chrome,args:["--no-sandbox"],headless: false});
const page = await browser.newPage();
page.setViewport({"width":1000,"height":1000})
fs.readFile(new URL(cookie_path), 'utf8', function(err,data){
    // Display the file content 
    var cookies = JSON.parse(data);
    var ccook = cookies.length;
    for (let cook in cookies){
    var cookie = cookies[cook];
//    console.log(cookie);
var setResult = page.setCookie(cookie);
//console.log("setResult",setResult);
if (cook == ccook-1){
setResult.then(()=>{page.goto(mainURL);
(async ()=>{
//await page.waitForNavigation({waitUntil:"networkidle0"});
// this is shit.
await page.waitForSelector('#editor');
await page.waitForSelector('.btn-box,.send-btn');
// wait for something else.
// do your shit.
console.log("network idle.");
// it is not.
async function mainLogic (index0){
//await page.waitForNavigation({waitUntil:"networkidle0"});
// this is shit.
await page.waitForSelector('#editor');
await page.waitForSelector('.btn-box,.send-btn');
console.log("network idle",index0);
var result = await page.evaluate(() => {
// just which page?
var editor = document.getElementById("editor");
var send = document.getElementsByClassName("btn-box send-btn")[0];
var editorpos = editor.getBoundingClientRect();
var sendpos = send.getBoundingClientRect();
var report = {"editor":JSON.stringify(editorpos),"send":JSON.stringify(sendpos)};
return report;
// execute it again?
// does not get shit.
})
// that is shit.
//console.log("result",result);
// better use api only, not this shit?
var thisTime = 0;
if (index0 == 0){
thisTime = 500;
/*
page.reload({waitUntil:"networkidle0"});
await page.waitForSelector('#editor');
await page.waitForSelector('.btn-box,.send-btn');
*/
};
setTimeout(()=>{

page.focus("#editor");

function rebuild(){

setTimeout(() => {if (index0 < midlist.length){page.goto(prefix+midlist[index0]);setTimeout(()=>{page.reload({waitUntil:"networkidle0"});mainLogic(index0+1);},750);}else{console.log("mission accomplished.");browser.close();process.exit(0);}},2000);};

if (index0!==0){
console.log("begin typing.");
page.type("#editor","hello world\n",{delay:50}).then(()=>{
console.log("finished typing.");
rebuild();});}else{
console.log("skip typing");
rebuild();
}

},thisTime);
// delay some time.
// and then repeat the parade.
// easy shit. now what?
// do shit with it.
};

setTimeout(() => {mainLogic(0)},1000);

})();
})
}
// this is promise.
    }
    // do extra.
}); 
})();
// set cookies first.
// const browser = puppeteer.launch({executablePath: exe_chrome});
